package com.market.web.controller;

import java.util.List;

import javax.faces.event.AbortProcessingException;
import javax.faces.event.ComponentSystemEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.market.domain.BaseObject;
import com.market.domain.Role;
import com.market.domain.User;
import com.market.manager.RoleManager;
import com.market.manager.UserManager;
import com.market.util.CollectionUtil;
import com.market.web.model.RoleModel;
import com.market.web.model.UserModel;

@Controller("roleController")
@Scope("session")
public class RoleController extends
		GenericController<RoleManager, RoleModel, Role> {
	
	@Autowired
	private UserModel userModel;

	@Override
	@Autowired
	protected void setModel(RoleModel model) {
		this.model = model;
	}

	@Override
	@Autowired
	protected void setManager(RoleManager manager) {
		this.manager = manager;
	}

	/*@Override
	public Role getNewObject() {
		Role role = new Role();
		role.setIsNew(true);
		return role;
	}*/

	
	/**
	 * Add all selected users to current role
	 * And clear selected propertied after this operation finish
	 * 
	 */
	public void addSelectedUsersToCurrent() {
		Role current = this.getModelCurrent();
		List<User> selectedUsers = userModel.getSelecteds();
		CollectionUtil<User> util = new CollectionUtil<User>();
		util.addAll(current.getUsers(), selectedUsers);
		util.clearSelection(selectedUsers);  //Normally we have to clear selected properties, after operation finish
	}
	
	/**
	 * Remove all selected users from current role
	 * And clear selected propertied after this operation finish
	 * 
	 */

	public void removeSelectedUsersFromCurrent() {
		List<User> users = this.getModel().getCurrent().getUsers();
		CollectionUtil<User> util = new CollectionUtil<User>();
		List<User> unSelectedUsers = util.extractUnSelected(users);
		this.getModelCurrent().setUsers(unSelectedUsers);
		util.clearSelection(users);		//Normally we have to clear selected properties, after operation finish
	}

}
